require'MiddleClass'
require'Stateful'
GV = class('GV'):include(Stateful)
require'randomlua'
require'TEsound'
require'util'
require'threading'
require'menu'
require'newgame'
require'town'
require'harvest'
require'maptools'
require'drawtools'
require'narrative'
require'player'
require'hud'
require'npc'
require'features'
require'items'
require'enemies'
require'gameplay'
require'animation'
require'victory'
require'defeat'




function clearLoveCallbacks()
  love.draw = nil
  love.joystickpressed = nil
  love.joystickreleased = nil
  love.keypressed = nil
  love.keyreleased = nil
  love.load = nil
  love.mousepressed = nil
  love.mousereleased = nil
  love.update = nil
end

function GV:initialize()
  super.initialize(self)
  self:gotoState('Menu')
end

function love.load()
	gv = GV:new()

	screen_w = love.graphics.getWidth()
	screen_h = love.graphics.getHeight()
	grid_size = 32
	tile_size = 16
	scale_size = 32

	map_x = 0
	map_y = 0
	map_offset_x = 0
	map_offset_y = 0

	love.keyboard.setKeyRepeat( 0.25, 0.125 )



	console_text = ""

	cur_map = {}

	small_font = love.graphics.setNewFont(8)
	norm_font = love.graphics.setNewFont(12)

	env_sheet_quads = {}
	env_sheet_size = 8

	harvest_maps = {}

	RNG = twister()

	grenade_thrown = {}
	gren_thrown_x = 0
	gren_thrown_y = 0
	draw_gren = false


	trade_win = love.graphics.newCanvas( (grid_size-4)*tile_size,
		(grid_size-4)*tile_size)

	player_fov = 3
	npc_fov = 4

	main_volume = 4

	show_enemy = {}

	enemy_counter = 0

	nerd_tar = {}
	nerd_counter = 0

	show_nar_image = false
	dbg_gis_img = false
	dbg_thread = 0

	threads = {}
	thread_buffer = {}
	temp_name = ""

	meta_tile_count = 0
	meta_tiles = {}
	threads_complete = 0

	grass_tile = 0
	wall_tile = 2
	floor_tile = 3
	sidewalk_tile = 4
	road_tile = 5
	table_tile = 6
	meta_tile = 7

	town_layout = {
		{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
		{2,4,4,4,4,4,4,4,4,5,5,4,4,4,4,4,4,4,4,4,4,4,4,4,4,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static_enemies()


	maps = {
		town = {
			name = "town",
			l_times = 0,
			tiles = {},
			npcs = {},
			features = {
				stair_dh = {
					tt = "stairs",
				},
			},
			items = {},
			--meta_tiles = {},
			layout = town_layout,
		},
	}
	data = {

		player = {
			image = packie_img,
			inventory = {

			},
			right_hand = {

			},
			left_hand = {
			},
			equip_mode = "none",
			buy_mode = false,
			drop_mode = false,
			targeting = false,
			text_input = false,
			con_text = "",
			merchant_id = 0,
			seeds = 25,
			bullets = 25,
			price_mod = 1,
			local_map = {},
			harvest_level = 0,
			current_harvest = 0,
			times_harvesting = 0,
			last_harvest = 0,
			turn = true,
			damage = 5,
			health = 100,
			max_health = 100,
			health_regen = .1,
			heal_amount = 25,
			heal_cost = 0.20,
			hup = true,
			xp = 0,
			req_xp = 100,
			level = 0,
			stamina = 100,
			max_stam = 100,
			stam_regen = 2,
			quests = {},
			drawX = (scale_size/2)*tile_size,
			drawY = (scale_size/2)*tile_size,
			impulse = "none",

		},
		fps = false,
	}

	t_cursor = {
		x = 0, y = 0, image = yellow_outl_img,

	}
	-- google image results deactivated here and in newgame.lua
	--spawn_meta_tiles("town", maps.town.meta_tiles)
end


function load_audio()
	p_step_aud = "audio/2step-tempo.mp3"
	gren_exp_aud = "audio/explosion-01.mp3"
	gunshot_aud = "audio/gun-gunshot-01.mp3"
	gun_cocking1_aud = "audio/gun-cocking-01.mp3"
	gun_cocking2_aud = "audio/gun-cocking-02.mp3"
	gun_cocking3_aud = "audio/gun-cocking-03.mp3"
	bullet_casing_fall_aud = "audio/empty-bullet-shell-fall-02.mp3"
	sad_car_horn_aud = "audio/cars014.mp3"

end

function load_images()
	-- images
	purple_img = love.graphics.newImage("img/purple.png")
	green_img = love.graphics.newImage("img/green.png")
	--may_img = love.graphics.newImage("img/ashleigh163.png")
	packie_img = love.graphics.newImage("img/16packie.png")
	seed_img = love.graphics.newImage("img/seed_pouch162.png")
	crabby_img = love.graphics.newImage("img/crabby16.png")
	arrow_img = love.graphics.newImage("img/arrow162.png")
	metal_img = love.graphics.newImage("img/16metal.png")
	portal_img = love.graphics.newImage("img/portal16.png")
	invis_img = love.graphics.newImage("img/blue_flower16.png")
	sidewalk_img = love.graphics.newImage("img/sidewalk162.png")
	road_img = love.graphics.newImage("img/black16.png")
	wood_img = love.graphics.newImage("img/brown16.png")
	wood_wall_img = love.graphics.newImage("img/wood_floor162.png")
	botanist_img = love.graphics.newImage("img/noob16.png")
	mayor_img = love.graphics.newImage("img/john_wayne16.png")
	green_man_img = love.graphics.newImage("img/greenMan16.png")
	human_f_img = love.graphics.newImage("img/human_f16.png")
	human_m_img = love.graphics.newImage("img/human_m16.png")
	quentin_img = love.graphics.newImage("img/paul162.png")
	space_ship_img = love.graphics.newImage("img/ship162.png")
	coyote1_img = love.graphics.newImage("img/coyote1-16.png")
	coyote2_img = love.graphics.newImage("img/coyote2-162.png")
	coyote3_img = love.graphics.newImage("img/coyote3-16.png")
	env_sheet_img = love.graphics.newImage("img/lofi_environment.png")
	env_sheet_img:setFilter("nearest","linear")

	yellow_outl_img = love.graphics.newImage("img/yellow_outline16.png")
	bgb_outl_img = love.graphics.newImage("img/16bgb_outline.png")

	apple_img = love.graphics.newImage("img/apple16.png")
	fade_brown_img = love.graphics.newImage("img/16fading_brown.png")
	grass_img = love.graphics.newImage("img/gras.png")
	brown_tree_img = love.graphics.newImage("img/16brown_tree.png")
	green_tree_img = love.graphics.newImage("img/16green_tree.png")
	brown_green_tree_img = love.graphics.newImage("img/16brown-green_tree.png")
	brown_grain_img = love.graphics.newImage("img/16brown_grain.png")
	axe_img = love.graphics.newImage("img/16axe.png")
	tree_stump_img = love.graphics.newImage("img/16stump.png")
	junk_img = love.graphics.newImage("img/16box.png")
	nurse_img = love.graphics.newImage("img/16nurse.png")
	bullet_img = love.graphics.newImage("img/16bullet-3.png")
	pistol_img = love.graphics.newImage("img/16pistol.png")
	reaper_img = love.graphics.newImage("img/16reaper.png")
	grass_spider_img = love.graphics.newImage("img/16grass_spider.png")
	pumpkin_img = love.graphics.newImage("img/veggie_4.png")
	tomato_img = love.graphics.newImage("img/veggie_0.png")
	carrot_img = love.graphics.newImage("img/veggie_3.png")
	strawberry_img = love.graphics.newImage("img/veggie_1.png")
	watermelon_img = love.graphics.newImage("img/veggie_7.png")
	cherry_img = love.graphics.newImage("img/veggie_10.png")
	pear_img = love.graphics.newImage("img/veggie_12.png")
	broccoli_img = love.graphics.newImage("img/veggie_15.png")
	potato_img = love.graphics.newImage("img/veggie_17.png")
	eggplant_img = love.graphics.newImage("img/veggie_21.png")
	radish_img = love.graphics.newImage("img/veggie_22.png")
	corn_img = love.graphics.newImage("img/veggie_23.png")
	grenade_img = love.graphics.newImage("img/16grenade.png")
	explode_img = love.graphics.newImage("img/explosion0.png")
	blood_splat_img = love.graphics.newImage("img/16blood_splatter.png")
	nerd_img = love.graphics.newImage("img/16nerd.png")
	computer_img = love.graphics.newImage("img/computer.png")
	computer_off_img = love.graphics.newImage("img/black-computer.png")
	bullet_pack_img = love.graphics.newImage("img/16bullet_pack.png")
	crater_img = love.graphics.newImage("img/16crater.png")

	politician_img = love.graphics.newImage("img/16politician.png")
	theone_img = love.graphics.newImage("img/16theone.png")
	morpheus_img = love.graphics.newImage("img/16theone-2.png")
	redpill_img = love.graphics.newImage("img/16red_pill.png")
	bluepill_img = love.graphics.newImage("img/16blue_pill.png")

	waterpump_img = love.graphics.newImage("img/16waterpump.png")
	-- stairs down
	env_sheet_quads[1] = love.graphics.newQuad(8 * env_sheet_size, 0 * env_sheet_size,
		env_sheet_size, env_sheet_size,
		env_sheet_img:getWidth(),env_sheet_img:getHeight())
	-- stairs up
	env_sheet_quads[4] = love.graphics.newQuad(7 * env_sheet_size, 0 * env_sheet_size,
		env_sheet_size, env_sheet_size,
		env_sheet_img:getWidth(),env_sheet_img:getHeight())
	-- floor
	env_sheet_quads[2] = love.graphics.newQuad(8 * env_sheet_size, 6 * env_sheet_size,
		env_sheet_size, env_sheet_size,
		env_sheet_img:getWidth(),env_sheet_img:getHeight())
	-- walls
	env_sheet_quads[3] = love.graphics.newQuad(8 * env_sheet_size, 5 * env_sheet_size,
		env_sheet_size, env_sheet_size,
		env_sheet_img:getWidth(),env_sheet_img:getHeight())

	--meta_tile_img = adj_noun_string()
end

function global_updates(dt)
	TEsound.cleanup()
	thread_update()

end

function reset_vars()

	maps = {
		town = {
			name = "town",
			l_times = 0,
			tiles = {},
			npcs = {},
			features = {
				stair_dh = {
					tt = "stairs",
				},
			},
			items = {},
			--meta_tiles = {},
			layout = town_layout,
		},
	}
	data = {

		player = {
			image = packie_img,
			inventory = {

			},
			right_hand = {

			},
			left_hand = {
			},
			equip_mode = "none",
			buy_mode = false,
			drop_mode = false,
			targeting = false,
			text_input = false,
			con_text = "",
			merchant_id = 0,
			seeds = 25,
			bullets = 25,
			price_mod = 1,
			local_map = {},
			harvest_level = 0,
			current_harvest = 0,
			times_harvesting = 0,
			last_harvest = 0,
			turn = true,
			damage = 5,
			health = 100,
			max_health = 100,
			health_regen = .1,
			heal_amount = 25,
			heal_cost = 0.20,
			hup = true,
			xp = 0,
			req_xp = 100,
			level = 0,
			stamina = 100,
			max_stam = 100,
			stam_regen = 2,
			quests = {},
			drawX = (scale_size/2)*tile_size,
			drawY = (scale_size/2)*tile_size

		},
		fps = false,
	}

	show_enemy = {}



end

